/*本模块功能:实验管理
        编写者:朱光曜
        编写时间:2017.11.15*/
$(function() {


    var string1 = '<tr>' +
        '<th class="col-lg-2">序号</th>' +
        '<th class="col-lg-4">姓名</th>' +
        '<th class="col-lg-5">学号</th>' +
        '<th class="col-lg-5">班级</th>' +
        '<th class="col-lg-7">打卡时间</th>' +
        '<th class="col-lg-2">状态</th>' +
        '</tr>';
    /*列表头部*/
    var confirm1 = new Confirm;
    var Alert1 = new Alert;
    var Alert2 = new Alert;
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });
    function locationInformationd(data) { //搜索
        var classes = ""; //班级
        var courses = ""; //课程
        // for(key in data.classList) {
        //     classes += '<option value="'+  data.classList[key].classId +'" classId="' + data.classList[key].classId + '">' + data.classList[key].className + '</option>';
        // }
        // for(key in data.courseList) {
        //     courses += '<option value="'+ data.courseList[key].courseId +'" courseId="' + data.courseList[key].courseId + '">' + data.courseList[key].courseName + '</option>';
        // }
        return '<div class="fl inputRight"><label for="departNames">状态:&ensp;</label>' +
            '<select id="selectCourse" class="publicSelect" >' +
            '<option value="正常">正常</option>' +
            '<option value="缺勤">缺勤</option> '+
            '</select>' +
            '</div>' +
            '<div class="fl inputRight"><label for="selectClass">日期:&ensp;</label>' +

            '<input type="date" name="user_date" class="datas"/>' +

            '</div>' +
            '<div class="fl publicBtn searchBtn">搜索</div>';
    }
        var ddd = new Date();
        var day =ddd.getDate();

        if(ddd.getMonth()<10){
            var month = "0"+(ddd.getMonth()+1);
        }

        if(ddd.getDate()<10){
            day = "0"+ddd.getDate();
        }

        var datew = ddd.getFullYear()+"-"+month+"-"+day;
        var datew = datew.toString();

       $('.datas').val(datew)

    function tabelModel(data) { //列表
       console.log(data,7777)
        var stuNums=data[key].stuNum;
        if(!stuNums){
            stuNums=0;
        }
        var homeworkNums=data[key].homeworkNum;
        if(!homeworkNums){
            homeworkNums=0;
        }
        var length = homeworkNums / stuNums  * 100;
        if(!stuNums&&!homeworkNums){
            length=0;
        }

        var aa = "";
        aa += '<tr data-num="0" chapterId="'+ data[key].chapterId +'" courseId="' + data[key].courseId + '" classId="' + data[key].classId + '" chapterNum="' + data[key].chapterNum + '">' ;
        aa +=  '<td class="col-lg-2">' +( parseInt(key) +1) + '</td>';
        aa +=   '<td class="col-lg-10"  title="' + data[key].username + '">' + data[key].username + '</td>';
        aa +=  '<td class="col-lg-10"  title="' + data[key].userNumber + '">' + data[key].userNumber + '</td>';
        aa +=  '<td class="col-lg-10"  title="' + data[key].className + '">' + data[key].className + '</td>' ;
        if ( data[key].clockTime == null) {
            aa +=   '<td class="col-lg-10"  title="">-</td>' ;
        } else {
            aa +=   '<td class="col-lg-10"  title="' + data[key].clockTime + '">' + data[key].clockTime + '</td>' ;
        }

        aa +=   '<td class="col-lg-10"  title="' + data[key].state + '">' + data[key].state + '</td>' ;

        return aa;

        // return
        //     '<td class="col-lg-2">' + ((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
        //     '<td class="col-lg-10"  title="' + data[key].username + '">' + data[key].username + '</td>' +
        //     '<td class="col-lg-10"  title="' + data[key].userNumber + '">' + data[key].userNumber + '</td>' +
        //     '<td class="col-lg-10"  title="' + data[key].className + '">' + data[key].className + '</td>' +
        //     '<td class="col-lg-10"  title="' + data[key].clockTime + '">' + data[key].clockTime + '</td>' +
        //     '<td class="col-lg-10"  title="' + data[key].state + '">' + data[key].state + '</td>' +
        //     '<td class="col-lg-4">' +
        //
        //     '</td>' +
        //     '</tr>';

    }
    $('.publicSelect').val('')
    var nowData = 1;
    var index = {
        some:'',
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                 "courseId":result.paramData.courseId,
                 "classIdList" :[result.paramData.classId],
                 "attendanceState" :'',
                 "date":datew
            };
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    this.parameter = result.paramData.some;
                    if( this.parameter.classId!=undefined){
                        $("#selectClass").val(this.parameter.classId);
                    }else{
                        $("#selectClass").children("option").eq(0).attr("selected",true);
                    }
                    if(this.parameter.courseId!=undefined){
                        $("#selectCourse").val(this.parameter.courseId);
                    }else{
                        $("#selectCourse").children("option").eq(0).attr("selected",true);
                    }
                }
            }

            self.some = this.parameter;
            this.postData(this.parameter, nowData);

        },



        postData: function(par_data, nowData, hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;


            $.ajax({
                type: "post",
                url: "attendance-mobile/query?pageNum=1&pageSize=200",
                contentType:"application/json;charset=utf-8",
                async: true,
                data: JSON.stringify(parameter),
                success: function(data, status, xhr) {
                   // console.log(data,33);

                    var new_data = data.data.list;
                    //console.log(new_data,1111)
                    if(data.code  == "200"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }
                        // console.log(new_data.totalPage+"啊啊"+new_data.pageNow)


                    }else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        add: function() { //搜索追加
            var self = this;
            var data = self.data_content;

            $("#searchCriteria").html(locationInformationd(data));
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    this.parameter = result.paramData.some;
                    if( this.parameter.classId!=undefined){
                        $("#selectClass").val(this.parameter.classId);
                    }else{
                        $("#selectClass").children("option").eq(0).attr("selected",true);
                    }
                    if(this.parameter.courseId!=undefined){
                        $("#selectCourse").val(this.parameter.courseId);
                    }else{
                        $("#selectCourse").children("option").eq(0).attr("selected",true);
                    }
                }
            }
            $(".funBox .searchBtn").off("click").click(function() { //搜索点击

                //点击搜索判断值
                // if($('.datas').val() == ''){
                //     alert('请输入日期')
                // }
                var ss = {
                    "courseId":result.paramData.courseId,
                    "classIdList" :[result.paramData.classId],
                    "attendanceState" :$('.publicSelect').val(),
                    "date": $('.datas').val()
                };
                $.ajax({
                    type: "post",
                    url: "attendance-mobile/query?pageNum=1&pageSize=200",
                    contentType:"application/json;charset=utf-8",
                    async: true,
                    data: JSON.stringify(ss),
                    success: function(data, status, xhr) {
                        //console.log(data,33);

                        var new_data = data.data.list;
                       // console.log(new_data,1111)
                        if(data.code  == "200"){

                                self.tab_content = new_data;

                                self.tab_add();



                        }else{
                            if(new_data.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }
                        }

                    },
                    error: function(data, status, xhr) {
                        var json = JSON.stringify({
                            "pageName": "error"
                        });
                        window.location.href = "skipPage?jsonStr=" + json;

                    }
                });
            //     var classid = [result.paramData.classId];
            //     var courseId = result.paramData.courseId;
            //     var data = $('.datas').val();
            //     var status = $('.publicSelect').val();
            //     console.log(888888888)
            //     var nowData = 1;
            //     var parameter = {
            //
            //         "classIdList":classid,
            //         "courseId": courseId,
            //         "status":status,
            //         "data":data,
            //         "attendanceState" : '',
            //         "pageNow": 1,
            //         "pageSize": 10
            //     };
            //     console.log(classid,88)
            //     //遍历删除空元素
            //     for(i in parameter){
            //
            //         if(parameter[i]==null||parameter[i]==""){
            //             delete parameter[i];
            //         }
            //         if(!isNaN(parameter[i])){
            //             parameter[i]=Number(parameter[i]);
            //         }
            //     }
            //
            //     self.some = parameter;
            //     self.postData(parameter, nowData, 1);
             });

        },
        tab_add: function() { //列表追加

            var self = this;
            var data = self.tab_content;
            var table = string1;

            //data.selectList={};

            var i = 0;
            for(key in data) {
                i++;
            }

            if(i > 0) {

                for(key in data) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);
            } else {
                var trImg = "<tr><td colspan='6'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
                $(".mainContent .tableBox").html(string1 + trImg);
            }

            //设置列表宽度(最大为9)
            var numMax = 0;
            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));
                numMax += num1;
            });

            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));

                $(this).css("width", num1 / numMax * 100 + "%");
            });




        },
        alert1: function(content1) { //confirm提示

            Alert1.contenthead = "提示";
            Alert1.content = content1;

            Alert1.success = function() {
                window.location.reload();
                Alert1.hide();
            };

            Alert1.init();
            Alert1.show();
        }
    };

    index.init();

});